package tetris;

import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextArea;

/**
 * Creates a panel with top ten results.
 * @author dejvid
 *
 */
public class HighScores extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel ground;
	private JTextField text;
	private JButton buttonReturn;
	private JTextArea scoresArea;

	/**
	 * Creates new panel which contains top ten results.
	 */
	public HighScores() {
		setTitle("Tetris");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setSize(300,600);
		setLocationRelativeTo(null);
			
		ground = new JPanel();
		ground.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				buttonReturn.setBackground(Color.ORANGE);
			}
		});
		ground.setBorder(new EmptyBorder(5, 5, 5, 5));
		ground.setBackground(Color.DARK_GRAY);
		ground.setLayout(null);
		ground.setDoubleBuffered(true);
		setContentPane(ground);
		
		text = new JTextField();
		text.setBounds(6, 18, 290, 74);
		text.setEditable(false);
		text.setFont(new Font("OCR A Std", Font.PLAIN, 34));
		text.setBackground(Color.DARK_GRAY);
		text.setForeground(Color.WHITE);
		text.setText("HIGH SCORES");
		text.setColumns(10);
		text.setBorder(null);
		text.setFocusable(false);
		text.setHorizontalAlignment(JTextField.CENTER);
		text.setDoubleBuffered(true);
		ground.add(text);
		
		buttonReturn = new JButton("RETURN");
		buttonReturn.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				buttonReturn.setBackground(Color.RED);
			}
		});
		buttonReturn.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
					returnToMainMenu();
			}
		});
		buttonReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				returnToMainMenu();
			}
		});
		buttonReturn.setOpaque(true);
		buttonReturn.setFont(new Font("Courier", Font.BOLD, 23));
		buttonReturn.setDoubleBuffered(true);
		buttonReturn.setBorder(null);
		buttonReturn.setBackground(Color.ORANGE);
		buttonReturn.setBounds(72, 484, 155, 43);
		ground.add(buttonReturn);
		
		ScoresSystem sm = new ScoresSystem();
		
		scoresArea = new JTextArea();
		scoresArea.setEditable(false);
		scoresArea.setForeground(Color.WHITE);
		scoresArea.setBackground(Color.DARK_GRAY);
		scoresArea.setFont(new Font("OCR A Std", Font.PLAIN, 15));
		scoresArea.setText( sm.getTopTen() );
		scoresArea.setLineWrap(true);
		scoresArea.setBounds(22, 151, 274, 257);
		scoresArea.setFocusable(false);
		ground.add(scoresArea);
		
	}
	
	/**
	 * Returns to Main Menu.
	 */
	public void returnToMainMenu() {
		GameMenu.run();
		dispose();
	}

	/**
	 * Makes top ten results panel's visible.
	 */
	public static void run() {
		HighScores h = new HighScores();
		h.setVisible(true);
	}
	
	public static void main(String[] args) {
		run();
	}
}
